package io;

import java.io.File;
import java.io.IOException;
import java.util.Collection;

import org.apache.commons.io.FileUtils;

public class FileToUTF8 {

	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		//GBK编码格式源码路径 
		String srcPath = "E:/workspeaces/LifeFly/ssh"; 
		  
		String[] fileTypes = new String[]{"java","xml","properties"};
		
		String srcFormt ="GBK";
		
		String toFormt ="UTF-8";
		
		transition(srcPath, srcFormt, toFormt, fileTypes);
		

	}
	
	/**
	 * 将指定目录的指定类型文件由srcFormt格式转化为toFormt格式
	 * @param srcPath
	 * @param srcFormt
	 * @param toFormt
	 * @param fileTypes
	 */
	public static void transition(String srcPath,String srcFormt,String toFormt,String[] fileTypes){
		Collection<File> javaGbkFileCol =  FileUtils.listFiles(new File(srcPath), fileTypes, true); 
        
		for (File javaGbkFile : javaGbkFileCol) { 
		      //UTF8格式文件路径 
		      String toFilePath = srcPath+javaGbkFile.getAbsolutePath().substring(srcPath.length()); 
		       //使用GBK读取数据，然后用UTF-8写入数据 
		      try {
				FileUtils.writeLines(new File(toFilePath), toFormt, FileUtils.readLines(javaGbkFile, srcFormt));
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}        
		}
	}

}
